package edu.zhangxu.servlet;


import edu.zhangxu.dao.SolutionDao;
import edu.zhangxu.dao.SolutionDaoImp;
import edu.zhangxu.pojo.Result;
import edu.zhangxu.pojo.Solution;
import edu.zhangxu.util.JDBCUtils;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.sql.Connection;
import java.sql.ResultSet;

public class LoadCode extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setStatus(405);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int problemId = Integer.parseInt(req.getParameter("problemId"));
        int language = Integer.parseInt(req.getParameter("language"));
        resp.setContentType("text/html;charset=utf-8");
        resp.setHeader("Access-Control-Allow-Origin", "*");
        resp.setHeader("Access-Control-Allow-Methods", "POST");
        Connection con = null;
        Solution solution = new Solution();
        SolutionDao dao = new SolutionDaoImp();
        JSONObject resJson = null;
        try {
            con = JDBCUtils.getConnection();
            byte[] data = new byte[req.getContentLength()];
            DataInputStream in = new DataInputStream(req.getInputStream());
            in.readFully(data);
            in.close();
            JSONObject jo = JSONObject.fromObject(new String(data));
            String code = jo.getString("code");
            solution.setProblemId(problemId);
            solution.setLanguage(language);
            solution.setCode(code);
            solution.setJudger("Master");
            dao.insert(con, solution);
            Integer solutionID = dao.getId(con);
            System.out.println(solutionID);
            resJson = JSONObject.fromObject(new Result(0, String.valueOf(solutionID)));
        } catch (Exception e) {
            e.printStackTrace();
            resJson = JSONObject.fromObject(new Result(-1, e.toString()));
        } finally {
            resp.getWriter().print(resJson);
            JDBCUtils.closeConnection(con, null);
        }
    }
}

/**
 *         try {
 *             // 将上传的代码写入到磁盘
 *             File file = new File(problemPath+"/userSubmit/"+"Solution.java");
 *             FileWriter fw = new FileWriter(file);
 *
 *             StringBuilder content = new StringBuilder();
 *             byte[] b = new byte[1024];
 *             int lens;
 *             while ((lens = servletInputStream.read(b)) > 0) {
 *                 content.append(new String(b, 0, lens));
 *             }
 *             jo = JSONObject.fromObject(content.toString());
 *             fw.write((String)jo.get("code"));
 *             fw.close();
 *         } catch (Exception e) {
 *             List<String> details = new LinkedList<>();
 *             details.add("提交的代码写入磁盘时错误：");
 *             details.add(e.toString());
 *             System.out.println(e.getMessage());
 *             jo = JSONObject.fromObject(new RunResult("Server Error", details));
 *             JsonConfig jc = new JsonConfig();
 *             resp.getWriter().print(jo);
 *             return ;
 *         }
 *
 *         System.out.println("compile...");
 *         TestCode tc = new TestCode(problemPath);
 *         List<String> compileRes = tc.compilerCode();
 *         if(compileRes.size() == 0) { // 编译通过，无错误
 *             // System.out.println("run...");
 *             if(tc.runCode()) { // 运行代码
 *                 jo = JSONObject.fromObject(new RunResult("Accepted"));
 *                 // System.out.println("accept");
 *             } else {
 *                 jo = JSONObject.fromObject(new RunResult("Wrong"));
 *                 // System.out.println("error");
 *             }
 *         } else { // 编译未通过，有错误
 *             // System.out.println("compile failed...");
 *             jo = JSONObject.fromObject(new RunResult("Compile Error", compileRes));
 *         }
 *         resp.getWriter().print(jo);
 */
